<template>
    <div class="m-content">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="display: flex; flex-direction: column; height:100%">
        <el-row style="flex: 1">
            <el-col :span="12">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="ruleForm.title"></el-input>
                </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item  label="类别" prop="type">
                <el-select v-model="ruleForm.type" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              
            </el-col>
        </el-row>
        <el-row>
            <el-form-item label="摘要" prop="description">
                <el-input type="textarea" v-model="ruleForm.description"></el-input>
            </el-form-item>
        </el-row>
        <el-row style="flex: 10">
            <el-form-item label="内容" prop="content" style="height: 100%">
                <mavon-editor v-model="ruleForm.content" style="height: 100%"></mavon-editor>
            </el-form-item>
        </el-row>

        <el-row style="flex: 1">
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-row>
      </el-form>

    </div>
</template>

<script>

  import {getClass, getBlogById, blogUpdate} from "network/app"
  export default {
    name: "BlogEdit.vue",
    data() {
      return {
        ruleForm: {
          id: '',
          title: '',
          description: '',
          content: '',  
          type: null,
        },

        options:[],

        rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '请输入摘要', trigger: 'blur' }
          ],
          content: [
            { trequired: true, message: '请输入内容', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

            const _this = this
            blogUpdate(this.ruleForm)
              .then(res=>{
                console.log(res)
                _this.$alert('操作成功', '提示', {
                  callback: action => {
                    _this.$router.push("/console/blog")
                  }
                })
              })
            // this.$axios.post('/blog/edit', this.ruleForm, {
            //   headers: {
            //     "Authorization": localStorage.getItem("token")
            //   }
            // }).then(res => {
            //   console.log(res)
            //   _this.$alert('操作成功', '提示', {
            //     confirmButtonText: '确定',
            //     callback: action => {
            //       _this.$router.push("/blogs")
            //     }
            //   });

            // })

            

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      getClass(){
        getClass()
          .then(res=>{
            console.log(res)
            this.options = res.data.data
          })
      },
      getNoteById(id){
        const _this = this
        getBlogById(id)
          .then(res=>{
            const blog = res.data.data
            _this.ruleForm.id = blog.id
            _this.ruleForm.title = blog.title
            _this.ruleForm.description = blog.description
            _this.ruleForm.content = blog.content
            _this.ruleForm.type = blog.type
          })
      }
    },
    created() {
      this.getClass()
      const blogId = this.$route.params.blogId
      if(blogId){
        this.getNoteById(blogId)
      }

    }
  }
</script>

<style scoped>
  .m-content {
    text-align: center;
    height: 100%;
  }
</style>